package controlLayer;

import modelLayer.*;
import dbLayer.*;

import java.util.ArrayList;

public class ControlPerson {
	
	private Person pers;

	private static ControlPerson instance = null;
	
	public static ControlPerson getInstance()
	{
		if(instance == null)
		{
			instance = new ControlPerson();
			
		}
		return instance;
	}
	
	public ControlPerson()
	{
		pers = new Person();
	}
	
	public ArrayList<Person> findAllPeople()
	{
		System.out.println("Start of finding people");
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.getAllPeople();
		System.out.println(allPeople);
		return allPeople;
	}
	
	public ArrayList<Person> findByFName(String fName)
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonbyFName(fName);
		return allPeople;
	}
	public ArrayList<Person> findByLName(String lName)
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByLName(lName);
		return allPeople;
	}
	public ArrayList<Person> findByAge(int age)
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByAge(age);
		return allPeople;
	}
	public ArrayList<Person> findByID(int id)
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByID(id);
		return allPeople;
	}
	public ArrayList<Person> findBySwag()
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonBySwag();
		return allPeople;
	}
	public ArrayList<Person> findByYolo()
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByYolo();
		return allPeople;
	}
	public ArrayList<Person> findByCool()
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByCool();
		return allPeople;
	}
	public ArrayList<Person> findByLame()
	{
		IFDBPerson dbPerson = new DBPerson();
		ArrayList<Person> allPeople = new ArrayList<Person>();
		allPeople = dbPerson.searchPersonByLame();
		return allPeople;
	}
	public int updatePerson(String fName, String lName, int age, float totalSwag, float totalYolo, float totalCool, float totalLame)
	{
		IFDBPerson dbPerson = new DBPerson();
		Person person = new Person();
		person.setAge(age);
		person.setfName(fName);
		person.setlName(lName);
		person.setTotalCool(totalCool);
		person.setTotalLame(totalLame);
		person.setTotalSwag(totalSwag);
		person.setTotalYolo(totalYolo);
		return dbPerson.updatePerson(person);
	}
	public void insertNewPerson(String fName, String lName, int age, float totalSwag, float totalYolo, float totalCool, float totalLame )
	{
		Person person = new Person();
		person.setAge(age);
		person.setfName(fName);
		person.setlName(lName);
		person.setTotalCool(totalCool);
		person.setTotalLame(totalLame);
		person.setTotalSwag(totalSwag);
		person.setTotalYolo(totalYolo);
		
		try
		{
			DBConnection.startTransaction();
			IFDBPerson dbPerson = new DBPerson();
			dbPerson.insertPerson(person);
			DBConnection.commitTransaction();
		} // end try
		catch (Exception e)
		{
			System.out.println("Could not insert new person, error : " + e);
			DBConnection.rollbackTransaction();
		}// end catch
	}
	public int deletePerson(int id)
	{
		IFDBPerson dbPerson = new DBPerson();
		return dbPerson.deletePerson(id);
	}
	public void setfName(String fName)
	{
		pers.setfName(fName);
	}
	public void getName()
	{
		pers.getfName();
	}
	public void setlName(String lName)
	{
		pers.setlName(lName);
	}
	public void getlName()
	{
		pers.getlName();		
	}
	public void setAge(int age)
	{
		pers.setAge(age);
	}
	public void getAge()
	{
		pers.getAge();
	}
}
